package com.accp.demo.service.purchase;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.accp.demo.mapper.base.IMaterialMapper;
import com.accp.demo.mapper.base.ISupplierMapper;
import com.accp.demo.mapper.pruchase.IPurchaseInstorageMapper;
import com.accp.demo.mapper.stock.IStockReportoryDetailMapper;
import com.accp.demo.pojo.PageBean;
import com.accp.demo.pojo.base.Material;
import com.accp.demo.pojo.base.Supplier;
import com.accp.demo.pojo.purchase.PurchaseInstorage;
import com.accp.demo.pojo.purchase.PurchaseInstorageDetail;
import com.accp.demo.pojo.stock.StockReportoryDetail;

@Service
public class PurchaseInstorageService {

	@Autowired
	private IPurchaseInstorageMapper iPurchaseInstorageMapper;
	@Autowired 
	private IStockReportoryDetailMapper iStockReportoryDetailMapper;
	@Autowired 
	private IMaterialMapper materielDaoMapper;
	@Autowired
	private ISupplierMapper iSupplierMapper;
	
	
	
	//加载单号
	public String findNo(String billDate){
		
		String billNo=null;
		billNo=billDate.replaceAll("-", "");
		System.out.println("biln："+billNo);
		PurchaseInstorage pur=iPurchaseInstorageMapper.isExist(billNo);

		
		if(pur==null){
			return billNo+"001";
		}
		System.out.println("单号:"+billNo.toString());
		return Long.parseLong(pur.getBillNo())+"1";
	}
	
	//分页查询
	public PageBean findByPage(Map<String,Object> map){
		int currentPage=Integer.valueOf(String.valueOf(map.get("page")));
		int pageSize=Integer.valueOf(String.valueOf(map.get("limit")));
		int count=iPurchaseInstorageMapper.count();
		//将currentPage修改为数据库所需的当前页数参数
		map.put("currentPage",(currentPage-1)*pageSize);
		map.put("pageSize", pageSize);
		System.out.println("我现在准备查询数据了!!!  查的是第"+currentPage+"页"+"，这一页有"+pageSize+"条记录！");
		List<PurchaseInstorage> list=iPurchaseInstorageMapper.findByPage(map);
		System.out.println("list："+list.get(0).toString());
		//List<PurchaseInstorage> list=iPurchaseInstorageMapper.findByPage((currentPage-1)*pageSize,pageSize);
		
		PageBean page=new PageBean(pageSize, count, currentPage, list);
		
		return page;
	}
	
	/**
	 * 新增或者修改
	 * @param purchaseInstorage
	 */
	public void insert(Map<String,Object> map){
		
		
		String billNo=(String) map.get("billNo");
		
		List<PurchaseInstorageDetail> list=(List<PurchaseInstorageDetail>) map.get("detail");
		System.out.println("进入新增service:");
		PurchaseInstorage is=iPurchaseInstorageMapper.isExist(billNo);
		
		if(is!=null){
			//修改，先删除详表数据，再新增
			iPurchaseInstorageMapper.deleteDetail(billNo);
			iPurchaseInstorageMapper.update(map);
			
		}else{
			//新增
			iPurchaseInstorageMapper.insert(map);
		}
		
			//新增详表数据
		
		
		for (PurchaseInstorageDetail item : list) {
			System.out.println(item.toString());
			//item.setBillNo(purchaseInstorage.getBillNo());
			iPurchaseInstorageMapper.insertDetail(item);
		}
	}
	/**
	 * 逻辑删除主详，修改状态为0
	 * @param lineID
	 * @param billNo
	 */
	public void logicdelete(String billNo){
		System.out.println("进入删除service:"+billNo);
		
		iPurchaseInstorageMapper.delete(billNo);
	}
	
	/**
	 * 审核反审核
	 * @param purchaseInstorage 封装了 purchaseInstorage 
	 */
	public void audit(PurchaseInstorage purchaseInstorage){
		//PurchaseInstorage purchaseInstorage=(PurchaseInstorage)(map.get("PurchaseInstorage"));
		//List<StockReportoryDetail> stockReportoryDetails=(List<StockReportoryDetail>)(map.get("StockReportoryDetails"));
		if(purchaseInstorage.getAuditState()==1){
		//进入审核
			//审核入库单，修改状态，审核时间
			iPurchaseInstorageMapper.audit(purchaseInstorage.getBillNo());
			//查询是否存在经入库单反审核后的库存台账记录，是就修改，否新增(错)
		
			//新增
			for (PurchaseInstorageDetail item1 : purchaseInstorage.getPurchaseInstorageDetails()) {
				//StockReportoryDetail stockReportoryDetail=iStockReportoryDetailMapper.findById(item1.getInfosId());
			/*	if(stockReportoryDetail!=null){
					//修改库存明细记录
					
					iStockReportoryDetailMapper.update(item1);
				}else{*/
				
				//新增库存台账明细记录
				{
					StockReportoryDetail stockReportoryDetail1=new StockReportoryDetail();
					stockReportoryDetail1.setGetAmount(item1.getQuantity());
					stockReportoryDetail1.setEndAmount(item1.getMaterial().getCattAmt()-item1.getQuantity());
					stockReportoryDetail1.setMaterialPrice(item1.getPrice());
					stockReportoryDetail1.setMateriel(item1.getMaterial());
					stockReportoryDetail1.setOrderForm(item1.getOrderForm());
					stockReportoryDetail1.setOrderNoForm(item1.getOrderNoForm());
					stockReportoryDetail1.setWareHouse(purchaseInstorage.getWareHouse());
				
					iStockReportoryDetailMapper.insert(stockReportoryDetail1);
				}
				/*}*/
			}
			
		
			//改变物料现有库存，成本单价(分仓核算成本)
			{
			Material materiel = null;
			
			for (PurchaseInstorageDetail item2 : purchaseInstorage.getPurchaseInstorageDetails()) {
				
				/*//分摊费用
				if(item2.getApportion()!=null){*/
				
						//现行平均成本=（上次结存实际成本+本次入库实际成本）/（上次结存数量+本次入库数量）
					//上次入库的库存明细表
					//StockReportoryDetail stockReportoryDetail3=iStockReportoryDetailMapper.findLastDetail(purchaseInstorage.getWareHouse().getWareNo(), item2.getMateriel().getMaterialNo());
					
					//该物料现行信息
					Material materiel2=materielDaoMapper.queryMatByNo(item2.getMaterial().getMaterialNo());
					
					//入库后的现行成本
					Double cavgCost=(materiel2.getCavgCost()*materiel2.getCattAmt()+item2.getQuantity()*item2.getPrice())/(materiel2.getCattAmt()+item2.getQuantity());
							
					materiel=new Material();
					materiel.setCattAmt(item2.getQuantity());
					materiel.setCavgCost(cavgCost);
					materiel.setcTotalCost(cavgCost*(materiel2.getCattAmt()+item2.getQuantity()));
					materiel.setBoutStockDay(purchaseInstorage.getBillDate());
					
					
					
				/*}*/
			}
			
			materielDaoMapper.updatemateriel(materiel);
			}
			
			//修改供应商账款额度，期末应付款
			{
				Supplier supplier=null;
				//根据编号查询供应商
				//Supplier supplier1=iSupplierMapper.findByNo();无此方法
				//期末应付款
				//supplier.setCurReceivable(curReceivable);	//
				//账款额度
				//supplier.setAmountQuota(amountQuota);
				//剩余额度
				//supplier.setRemainQuota(remainQuota);
				//最近入库日
				//supplier.setLatelyTradeDate(latelyTradeDate);
				iSupplierMapper.update(supplier);
				
			}
			
			//新增供应商账款明细记录
			{
				
				
				
			}
			//修改订单未入库量
			{
				
				
				
				
			}
		}else{
		//进入反审核
			//反审核入库单，修改状态，审核时间
			iPurchaseInstorageMapper.unAudit(purchaseInstorage.getBillNo());
			//逻辑删除库存台账明细记录
			/*for (StockReportoryDetail item3 : stockReportoryDetails) {
				iStockReportoryDetailMapper.delete(item3.getInfosId());
			}*/
			
		}
		
		
	}
}
